%authors: Adriana and Edward
%last edited: 4/19/19
%
% Modified by David Sorensen
%	-add condition_tag to allow for mutliple conditional breakdowns
%
%script to recode events in GE in order to train and test SVM on different
%conditions. 

num_of_blocks=16;
% neighbors_vs_seeds=0; 
% neighbors_vs_seeds=1; %set this variable to 1 if you want to categorize events by all neighbors and all seeds, regardless of neighbor type
% lexical_and_nonword_neighbors_vs_seeds=0;
% lexical_and_nonword_neighbors_vs_seeds=1; %set this variable to 1 if you want to categorize events by lexical neighbors, non-word neighbors, and seeds.  
condition_tag = 'pos'; % 'nvs'|'lvn'|'pos'|'biphone'

%%Edit to automatically go through all subjects
Subjects_List = {'GE_05', 'GE_06', 'GE_07', 'GE_08', 'GE_09', 'GE_10', 'GE_11', 'GE_12', 'GE_13', 'GE_14', 'GE_15', 'GE_16','GE_17','GE_18','GE_19','GE_20','GE_21','GE_22','GE_23','GE_24'};
for subject=1:length(Subjects_List)
subject_id = Subjects_List{subject};

for block=1:num_of_blocks
            offset = 0;
    %filenames{block}=sprintf('/autofs/space/clive_001/users/adriana/GE/MEG/%s/events/%s_B%d_grouped.eve'...
    %    ,subject_id,subject_id, block);
   filenames{block}=sprintf('/autofs/space/clive_001/users/olivia/GE_new/MEG/%s/events/%s_B%d_grouped.eve'...
        ,subject_id,subject_id, block);
    %load events for block x
    og_events=load(filenames{block});
    new_events=NaN(size(og_events,1),size(og_events,2));
   
    for eventnum=2:length(og_events)
        event=int32(og_events(eventnum,4));
        new_event=NaN;
        if event==4294934528 || event==16384
            continue
        else
            ones_place=mod(event,10);
            hundreds_place=mod(idivide(event,int32(100)),10);
            tens_place=mod(idivide(event,int32(10)),10);
            if hundreds_place > 3 || tens_place > 3 || ones_place > 6
                continue
            end
            %event_minus_1000=mod(event, 1000);
            if hundreds_place==2 || hundreds_place==3 %if word or non-word neighbor
                switch condition_tag
                    case 'nvs'
                    switch ones_place
                        case 1
                            new_event=11; %pig neighbors
                        case 2
                            new_event=22; %toad neighbors
                        case 3
                            new_event=33; %cab neighbors
                        case 4
                            new_event=44; %bike neighbors
                        case 5
                            new_event=55; %dupe neighbors
                        case 6
                            new_event=66; %gut neighbors
                    end %switch ones_place neighbors_vs_seeds
                
                    case 'lvn'
                    switch hundreds_place
                        case 2 %word neighbor
                            switch ones_place
                                case 1
                                    new_event=10; %pig word neighbors
                                case 2
                                    new_event=20; %toad word neighbors
                                case 3
                                    new_event=30; %cab word neighbors
                                case 4
                                    new_event=40; %bike word neighbors
                                case 5
                                    new_event=50; %dupe word neighbors
                                case 6
                                    new_event=60; %gut word neighbors
                            end %switch ones_place
                        case 3 %nonword neighbor
                            switch ones_place
                                case 1
                                    new_event=100; %pig nonword neighbors
                                case 2
                                    new_event=200; %toad nonword neighbors
                                case 3
                                    new_event=300; %cab nonword neighbors
                                case 4
                                    new_event=400; %bike nonword neighbors
                                case 5
                                    new_event=500; %dupe nonword neighbors
                                case 6
                                    new_event=600; %gut nonword neighbors
                            end %switch ones_place
                    end %switch hundreds_place lexical_and_nonword_neighbors_vs_seeds
                    case 'pos'
                        switch tens_place
							case 1
                            	switch ones_place
                                	case 1
                                    	new_event = 1001; %_ig neighbors
                                	case 2
                                    	new_event = 2001; %_oad neighbors
                                	case 3
                                	    new_event = 3001; %_ab neighbors
                                	case 4
                                	    new_event = 4001; %_ike neighbors
                                	case 5
                                	    new_event = 5001; %_upe neighbors
                                	case 6
                                	    new_event = 6001; %_ut neighbors
                            	end
                        	case 2
                            	switch ones_place
                                	case 1
                                	    new_event = 1002; %p_g neighbors
                                	case 2
                                	    new_event = 2002; %t_d neighbors
                                	case 3
                                	    new_event = 3002; %c_b neighbors
                                	case 4
                                	    new_event = 4002; %b_k neighbors
                                	case 5
                                	    new_event = 5002; %d_p neighbors
                                	case 6
                                	    new_event = 6002; %g_t neighbors
                            	end
							case 3
								switch ones_place
									case 1
                                	    new_event = 1003; %pi_ neighbors
                                	case 2
                                	    new_event = 2003; %to_ neighbors
                                	case 3
                                	    new_event = 3003; %ca_ neighbors
                                	case 4
                                	    new_event = 4003; %by_ neighbors
                                	case 5
                                	    new_event = 5003; %doo_ neighbors
                                	case 6
                                	    new_event = 6003; %gu_ neighbors
                            	end
							otherwise
								warning('No match to the tens_place %s %d', subject_id, block);
                        end
                   case 'biphone'
					   %switch hundreds_place
					       %case 2 %word neighbor
						       switch tens_place
									case 1
									     switch ones_place
											case 1
												new_event = 1201;
											case 2
												new_event = 2201;
											case 3
												new_event = 3201;
											case 4
												new_event = 4201;
											case 5
												new_event = 5201;
											case 6
												new_event = 6201;
										end
									case 3
										switch ones_place
											case 1
												new_event = 1202;
											case 2
												new_event = 2202;
											case 3
												new_event = 3202;
											case 4
												new_event = 4202;
											case 5
												new_event = 5202;
											case 6
												new_event = 6202;
										end
									case 2
										switch ones_place
											case 1
												new_event = [1201; 1202];
											case 2
												new_event = [2201; 2202];
											case 3
												new_event = [3201; 3202];
											case 4
												new_event = [4201; 4202];
											case 5
												new_event = [5201; 5202];
											case 6
												new_event = [6201; 6202];
										end
								end
						  % case 3 %nonword neighbor
                    		%	switch tens_place
							%		case 1
							%		     switch ones_place
							%				case 1
							%					new_event = 1301;
							%				case 2
							%					new_event = 2301;
							%				case 3
							%					new_event = 3301;
							%				case 4
							%					new_event = 4301;
							%				case 5
							%					new_event = 5301;
							%				case 6
							%					new_event = 6301;
									%	end
									%case 3
									%	switch ones_place
									%		case 1
									%			new_event = 1302;
										%	case 2
										%		new_event = 2302;
										%	case 3
										%		new_event = 3302;
										%	case 4
										%		new_event = 4302;
										%	case 5
											%	new_event = 5302;
											%case 6
											%	new_event = 6302;
										%end
								%	case 2
								%		switch ones_place
								%			case 1
								%				new_event = [1301; 1302];
								%			case 2
								%				new_event = [2301; 2302];
								%			case 3
								%				new_event = [3301; 3302];
								%			case 4
								%				new_event = [4301; 4302];
								%			case 5
								%				new_event = [5301; 5302];
								%			case 6
								%				new_event = [6301; 6302];
								%		end
								%end
				%		end

                end %switch condition_tag
                
            elseif hundreds_place==1 %code hub words
                new_event=ones_place;
            end %if statement to recode event

            if isnan(new_event)==false
                new_events(eventnum+offset,1:3)=og_events(eventnum,1:3);
                new_events(eventnum+offset,4)=new_event(1);
				if length(new_event) == 2
					offset = offset + 1;
					new_events(eventnum+offset,1:3)=og_events(eventnum,1:3);
					new_events(eventnum+offset,4) = new_event(2);
				end
                %behav_events(eventnum,1)=new_event;
            end
        end
        
        
    end %for each event
    
   %remove NaN values (removed events) from event list
   new_events=new_events(~any(isnan(new_events),2),:);
   %behav_events=behav_events(~any(isnan(behav_events),2),:);
   % create new event file in 'events' location with '_grouped' indicating
   %  completed processing
   filename=sprintf('/autofs/space/clive_001/users/adriana/GE_SVM/%s/events/%s_B%d_grouped_%s.eve',subject_id,subject_id,block,condition_tag);
   fid = fopen(filename, 'w'); %'w' is what allows it to create a new file
   
   %enter new events into 'grouped' eventfile 
   for i_event = 1:size(new_events, 1)
       fprintf(fid, '%6d%- 8.3f %6d %5d\n',...
           new_events(i_event, 1), new_events(i_event, 2), new_events(i_event, 3), new_events(i_event, 4));
   end % For Each event
   
   fclose(fid);
   
%     if plot_behav=='Y'
%         if block==1
%             allblock_events=behav_events(:,1);
%         else
%             allblock_events=vertcat(allblock_events,behav_events(:,1));
%         end %if block 1, create allblock_events. else, just add other block's events
%         
%     end %if plotting behav data=yes
   
   
   
   
end %for each block
end %for each subject
% %Extract behavioral results and plot as stacked bar graph
%     %note: coded all as separate objects for max flexibility for future
%     %behavioral result analyses
% if plot_behav=='Y'
%     %'Word' Stimulus
%     word=allblock_events(find(allblock_events<3000));
%     word_correctresp=allblock_events(find(allblock_events<2000)); %responded correctly
%     word_incorrectresp=allblock_events(find(allblock_events<3000 & allblock_events>2000)); %responded incorrectly
%     
%     %number of total events for good stim, good+yes response, good+no response
%     N_word=length(word)
%     N_word_correctresp=length(word_correctresp)
%     N_word_incorrectresp=length(word_incorrectresp)
%     
% 
%     
%     %'NonWord' stimulus
%     nonword=allblock_events(find(allblock_events>3000));
%     nonword_correctresp=allblock_events(find(allblock_events<4000 & allblock_events>3000));%responded correctly
%     nonword_incorrectresp=allblock_events(find(allblock_events>4000));
%      %number of total events for good stim, good+yes response, good+no response
%     N_nonword=length(nonword)
%     N_nonword_correctresp=length(nonword_correctresp)
%     N_nonword_incorrectresp=length(nonword_incorrectresp)
%     
%     overall_percent_accuracy=(100*(N_word_correctresp+N_nonword_correctresp))/(N_word+N_nonword)
%     %plot as stacked bar graph
%     figure(1)
%     bar([N_word_correctresp N_word_incorrectresp; N_nonword_correctresp N_nonword_incorrectresp],'stacked');
%     legend('correct response','incorrect response');
%     title(sprintf(' word (1) vs. non-word (2) performance for GE%s \n %0.2f percent accuracy overall',participant_num,overall_percent_accuracy));
%     xlabel('stimulus');
%     ylabel('number of trials');
%     
%     %option to save figs as jpg once you've seen the image
%     savefigs=input('Do you want to save the figure as a JPG?(Y or N)');
%         %  savefigs='N';
%         %  savefigs='Y';
%     if savefigs=='Y'
%         %format/resize fig to fill screen for continuity
%         fignum=1;
%         figSize = [58, 48];            % [width, height]
%         figUnits = 'Centimeters';
%         set(fignum, 'Units', figUnits);
%         pos = get(fignum, 'Position');
%         pos = [pos(1)+figSize(1), pos(4)+figSize(2), pos(3)+figSize(1), pos(4)+figSize(2)];
%         set(fignum, 'Position', pos); %fill screen
%         print_fignum=sprintf('-f%d',fignum);
%         fig_save_name=sprintf('/autofs/space/clive_001/users/adriana/GE/MEG/GE_%s/behavioralresults_GE%s',participant_num,participant_num); %name of jpg
%         print(print_fignum,fig_save_name,'-djpeg'); %save jpg
%         
%     end %if you want the figure jpgs stored
% end %if plotting behav data='Y' ie yes

